package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 *	1378
 * 太原市供电局	http://www.ty.sx.sgcc.com.cn/
 * @author KK
 *
 */
public class SgccNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.ty.sx.sgcc.com.cn/articles/[0-9]+/a[0-9]*.html",
		"http://www.sh.sgcc.com.cn/FSM_CMS/html/main/.*/[0-9]*-[0-9]*/[0-9]*/[0-9]*_[0-9]*.html",
		"http://.*.sgcc.com.cn/.*html/main/col[0-9]*/[0-9]{4}-[0-9]{2}/[0-9]{2}/[0-9]*_1.html",
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		if (url.startsWith("http://www.ty.sx.sgcc.com.cn/")){
			title =doc.select("title").text();
			if (StringUtils.isNullOrEmpty(title)){
//				title =doc.select("div.allnrtitle").text();
//				if (title!=null)title=StringUtils.substringBefore(title, "_");
			}

			content=HtmlCleaner.getContentHtml(url,doc.select("div.wenzhang"));
			date=DateUtils.matchDate(doc.select("td:contains(发布时间)").text());
			if (date==null){
				date=DateUtils.matchDate(doc.select("div.nrbq").text());
			}
		}else if(url.startsWith("http://www.sh.sgcc.com.cn")){
			title =doc.select("h1").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.cont"));
			date=DateUtils.matchDate(doc.select("div.info").text());
		}else if(url.startsWith("http://www.fj.sgcc.com.cn/")){
			title =doc.select("div.txtcon h1").text();
			date=DateUtils.matchDate(doc.select("div.txtcon div.info").text());
			content =doc.select("div.txtcon div.cont").text();
		}else if(url.startsWith("http://www.jx.sgcc.com.cn")){
			title=doc.select("h1 span").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.cont"));
			date=DateUtils.matchDate(doc.select("div.info").text());
		}
		if (StringUtils.isBlank(title) || date==null){

		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);
		news.setType(1);
		return news;
	}

	public static void main(String[] args) {
		SgccNewsAnalyse analyse=new SgccNewsAnalyse();
		String url="http://www.jx.sgcc.com.cn/html/main/col7/column_7_1.html";
		System.out.println(DoMainUtils.GetDomainName(url));
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
